<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button preIcon="ant-design:clear-outlined" type="primary" @click="handleClean" style="margin-right: 5px"
          danger>清空</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <JobLogDesc @register="registerDesc"></JobLogDesc>
  </div>
</template>
<script lang="ts" name="job-log" setup>
import { ref } from 'vue';
import { BasicTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useDrawer } from '/@/components/Drawer';
import { useListPage } from '/@/hooks/system/useListPage'
import { getQuartzList, cleanJobLog, deleteQuartz, batchDeleteQuartz, getExportUrl, getImportUrl } from './api';
import { columns, searchFormSchema } from './data';
import JobLogDesc from './components/JobLogDesc.vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { useRoute, useRouter } from "vue-router"
import { Icon } from '/@/components/Icon';

const router = useRouter()
let jobId = router.currentRoute.value.query?.jobId
console.log("currentRoute.params: " , jobId);

const { createMessage } = useMessage();
const [registerDesc, { openDrawer: openJobLogDesc }] = useDrawer();
// 列表页面公共参数、方法
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  designScope: 'quartz-job-log',
  tableProps: {
    title: '任务日志列表',
    api: getQuartzList,
    beforeFetch:(params)=>{
      params.jobId = jobId
    },
    columns: columns,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      slots: { customRender: 'action' },
    },
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']],
    },
  },
  exportConfig: {
    name: "定时任务日志列表",
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl
  }
})


const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

/**
 * 操作列定义
 * @param record
 */
function getActions(record) {
  return [
    {
      label: '详情',
      onClick: handleDesc.bind(null, record),
    },
    {
      label: '删除',
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      }
    }
  ];
}

/**
 * 清空
 */
function handleClean() {
  cleanJobLog(reload)
}

/**
 * 详情
 */
function handleDesc(record) {
  openJobLogDesc(true, {
    record,
    isUpdate: true,
  });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteQuartz({ id: record.id }, reload);
}


/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  console.log("selectedRowKeys: ", selectedRowKeys.value);
  await batchDeleteQuartz({ ids: selectedRowKeys.value }, reload);
}
</script>
